package cave;

import java.util.ArrayList;

import android.graphics.Color;
import android.util.Log;
import cave.game.GameServices;
import cave.game.InputEvent;
import cave.game.MainActivity;
import cave.game.Orientation;
import cave.game.Screen;
import cave.layer.Layer;
import cave.layer.LayerManager;
import cave.renderer.RenderList;
import cave.ui.Button;
import cave.ui.ButtonListener;
import cave.ui.CheckBox;
import cave.ui.CheckBoxListener;
import cave.ui.Dpad;
import cave.ui.Image;
import cave.ui.TextField;
import cave.ui.UiContainer;

public class MainMenuLayer implements Layer {
	
	public final static String LAYER_ID = "MainMenu";
	
	private final LayerManager layerMgr;
	private GameServices gameServices;
	private UiContainer container;
	
	private int clickCount;
	private TextField tfClickText, tfClickCount;
	
	private Dpad dpad;
	private Image movingImage, movingImage2;
	
	private int frames;
	private float fpsTime, fpsStart;
	private TextField tfFps, tfFpsText, tfRfps, tfRfpsText;
	
	private TextField tfOrientationX, tfOrientationY, tfOrientationZ;

	public MainMenuLayer(final LayerManager layerMgr, GameServices gameServices) {
		this.layerMgr = layerMgr;
		this.gameServices = gameServices;
	}

	@Override
	public void onCreate() {
		clickCount = gameServices.getSharedPrefs().getInteger("mmCount", 0);

		container = new UiContainer();
		
		// Dpad
		dpad = new Dpad(0.05f, 0.05f, 0.4f, 
				gameServices.getTextureManager().getTexture("dpad_bg"), 
				gameServices.getTextureManager().getTexture("dpad_but"));
		container.add(dpad);
		
		container.add(new TextField(0.0f, 0.9f, "MainMenuLayer:",
				gameServices.getTextureManager().getFont("medium"), 0.1f, Color.GREEN));
		
		tfClickText = new TextField(0.0f, 0.8f, "Click count: ",
				gameServices.getTextureManager().getFont("medium"), 0.1f, Color.RED);
		container.add(tfClickText);
		
		tfClickCount = new TextField(tfClickText.getLength(), 0.8f, "",
				gameServices.getTextureManager().getFont("medium"), 0.1f, Color.RED);
		tfClickCount.setInt(clickCount);
		container.add(tfClickCount);
						
		container.add(new Button(0.5f*Screen.getAspectRatio()-0.2f, 0.3f, 0.4f, 0.4f,
				gameServices.getTextureManager().getTexture("but_g_up"),
				gameServices.getTextureManager().getTexture("but_g_down"),
				gameServices.getTextureManager().getTexture("but_g_dis"),
				new ButtonListener() {
					@Override
					public void actionClick() {
						clickCount++;
						gameServices.getSharedPrefs().addInteger("mmCount", clickCount);
						tfClickCount.setInt(clickCount);
					}
				}
		));
		
		container.add(new Button(0.8f*Screen.getAspectRatio(), 0.1f, 0.2f, 0.2f,
				gameServices.getTextureManager().getTexture("but_g_up"),
				gameServices.getTextureManager().getTexture("but_g_down"),
				gameServices.getTextureManager().getTexture("but_g_dis"),
				new ButtonListener() {
					@Override
					public void actionClick() {
						layerMgr.openLayer(OptionsMenuLayer.LAYER_ID);
						layerMgr.closeLayer(LAYER_ID);
					}
				}
		));
		
		// CheckBox
		container.add(new CheckBox(0.8f*Screen.getAspectRatio(), 0.8f, 0.2f, 0.2f, 
				gameServices.getTextureManager().getTexture("testi1"), 
				gameServices.getTextureManager().getTexture("testi2"), 
				gameServices.getTextureManager().getTexture("testi3"),
				gameServices.getTextureManager().getTexture("testi4"),
				false, 
				new CheckBoxListener() {
					@Override
					public void stateChanged(boolean newState) {
						Log.d(MainActivity.TAG, "MainMenuLayer: CheckBoxListener state changed");
					}
				}
		));
		
		movingImage = new Image(Screen.getAspectRatio()*0.5f, 0.5f, 0.1f, 0.1f, gameServices.getTextureManager().getTexture("testi1"));
		container.add(movingImage);
		
		// FPS CODE
		tfFpsText = new TextField(Screen.getAspectRatio()*0.5f, 0.2f, "fps: ", 
				gameServices.getTextureManager().getFont("medium"), 0.07f, Color.RED);
		container.add(tfFpsText);
		tfFps = new TextField(Screen.getAspectRatio()*0.5f + tfFpsText.getLength(), 0.2f, "", 
				gameServices.getTextureManager().getFont("medium"), 0.07f, Color.RED);
		container.add(tfFps);
		
		tfRfpsText = new TextField(Screen.getAspectRatio()*0.5f, 0.1f, "rfps: ", 
				gameServices.getTextureManager().getFont("medium"), 0.07f, Color.RED);
		container.add(tfRfpsText);
		tfRfps = new TextField(Screen.getAspectRatio()*0.5f + tfRfpsText.getLength(), 0.1f, "", 
				gameServices.getTextureManager().getFont("medium"), 0.07f, Color.RED);
		container.add(tfRfps);
		
		
		// orientation
		movingImage2 = new Image(Screen.getAspectRatio()*0.7f, 0.7f, 0.1f, 0.1f, gameServices.getTextureManager().getTexture("testi3"));
		container.add(movingImage2);
		
		tfOrientationX = new TextField(0.1f, 0.5f, "", 
				gameServices.getTextureManager().getFont("medium"), 0.07f, Color.GREEN);
		tfOrientationX.setFloat(0, 3);
		container.add(tfOrientationX);
		tfOrientationY = new TextField(0.1f, 0.6f, "", 
				gameServices.getTextureManager().getFont("medium"), 0.07f, Color.GREEN);
		tfOrientationY.setFloat(0, 3);
		container.add(tfOrientationY);
		tfOrientationZ = new TextField(0.1f, 0.7f, "", 
				gameServices.getTextureManager().getFont("medium"), 0.07f, Color.GREEN);
		tfOrientationZ.setFloat(0, 3);
		container.add(tfOrientationZ);
		
	}

	@Override
	public void onOpen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdate(float dt, ArrayList<InputEvent> inputEvents) {
		container.update(dt, inputEvents);

		float newX = movingImage.getX() + dpad.getVecX()*dt*2;
		float newY = movingImage.getY() + dpad.getVecY()*dt*2;
		
		if (newX > Screen.getAspectRatio() - movingImage.getWidth()) {
			newX = Screen.getAspectRatio() - movingImage.getWidth();
		} else if (newX < 0) {
			newX = 0;
		}
		
		if (newY > 1 - movingImage.getHeight()) {
			newY = 1 - movingImage.getHeight();
		} else if (newY < 0) {
			newY = 0;
		}
		
		movingImage.setX(newX);
		movingImage.setY(newY);
				
		
		// display orientation & move image
		tfOrientationX.setFloat(Orientation.getX(), 3);
		tfOrientationY.setFloat(Orientation.getY(), 3);
		tfOrientationZ.setFloat(Orientation.getZ(), 3);
				
		newX = movingImage2.getX() - dt*Orientation.getY();
		newY = movingImage2.getY() + dt*Orientation.getZ();
		
		if (newX > Screen.getAspectRatio() - movingImage2.getWidth()) {
			newX = Screen.getAspectRatio() - movingImage2.getWidth();
		} else if (newX < 0) {
			newX = 0;
		}
		
		if (newY > 1 - movingImage2.getHeight()) {
			newY = 1 - movingImage2.getHeight();
		} else if (newY < 0) {
			newY = 0;
		}
		
		movingImage2.setX(newX);
		movingImage2.setY(newY);

		
		// FPS CODE
		fpsTime += dt;
		if (fpsTime - fpsStart > 1.0f) {
			fpsStart = fpsTime;
			tfFps.setInt(frames);
			tfRfps.setInt(gameServices.getSharedPrefs().getInteger("rfps"));
			frames = 0;
		}
		frames++;
		
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDestroy() {
		container.destroy();
	}

	@Override
	public void draw(RenderList renderList) {
		container.draw(renderList);
	}

	@Override
	public String getId() {
		return LAYER_ID;
	}
 
}

